package com.noahplat.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.noahplat.common.utils.Result;
import com.noahplat.common.utils.StatusCode;
import com.noahplat.user.mapper.AccountMapper;
import com.noahplat.user.mapper.UserMapper;
import com.noahplat.user.pojo.Account;
import com.noahplat.user.pojo.User;
import com.noahplat.user.service.AccountService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 账套相关操作接口实现
 *
 * @author 李东
 * @version 1.0.0
 * @Date: 2021/3/1 15:14
 **/
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 查询账套列表
     *
     * @return
     * @author 李东
     * @version 1.0.0
     * @Date: 2021/3/1 15:20
     */
    @Override
    public Result findAccountList() {
        Example example = new Example(Account.class);
        Iterable<Integer> list = Arrays.asList(1);
        example.createCriteria().andNotIn("id", list);
        example.and(example.createCriteria().andEqualTo("status", "Y"));
        List<Account> accList = accountMapper.selectByExample(example);
        if (ObjectUtils.isEmpty(accList)) {
            return new Result(true, StatusCode.NOTFOUNDERROR, "未查询到账套信息！");
        }
        return new Result(true, StatusCode.OK, "查询成功！", accList);
    }

    /**
     * 通过Id查询账套信息
     *
     * @param id
     * @return
     * @author 李东
     * @version 1.0.0
     * @Date: 2021/3/1 15:20
     */
    @Override
    public Result<Account> findAccInfoById(Long id) {
        Example example = new Example(Account.class);
        example.createCriteria().andEqualTo("id", id);
        Account account = accountMapper.selectOneByExample(example);
        if (ObjectUtils.isEmpty(account)) {
            return new Result(true, StatusCode.NOTFOUNDERROR, "未查询到账套信息！");
        }
        return new Result(true, StatusCode.OK, "查询成功！", account);
    }

    /**
     * 分页查询账套信息
     *
     * @param status
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Result getAccount(String status, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        Example example = new Example(Account.class);
        if (!StringUtils.isEmpty(status)) {
            example.createCriteria().andEqualTo("status", status);
        }
        List<Account> list = accountMapper.selectByExample(example);
        if (ObjectUtils.isEmpty(list)) {
            return new Result(false, StatusCode.NOTFOUNDERROR, "暂无失败");
        }
        PageInfo<Account> pageInfo = new PageInfo<>(list);
        return new Result(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /**
     * 新增账套信息
     *
     * @param account
     * @return
     */
    @Override
    public Result addAccount(Account account) {
        User user = userMapper.selectByPrimaryKey(account.getUid());
        if (ObjectUtils.isEmpty(user)) {
            return new Result(false, StatusCode.ERROR, "创建人信息错误");
        }
        account.setCreatedBy(user.getName());
        account.setCreatedTime(new Date());
        int res = accountMapper.insertSelective(account);
        if (res == 0) {
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /**
     * 修改账套信息
     *
     * @param account
     * @return
     */
    @Override
    public Result updateAccount(Account account) {
        int res = accountMapper.updateByPrimaryKeySelective(account);
        if (res == 0) {
            return new Result(false, StatusCode.ERROR, "修改失败");
        }
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 不分页获取账套信息
     *
     * @return
     */
    @Override
    public Result getAccounts() {
        List<Account> list = accountMapper.selectAll();
        if (ObjectUtils.isEmpty(list)) {
            return new Result(false, StatusCode.ERROR, "暂无数据");
        }
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

}
